<template>
<!-- 评论组件 -->
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p><span>{{commentInfo.salesCount}}</span><span>人购买</span></p>
        <p><span>{{commentInfo.praisePercent}}</span><span>好评率</span></p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a href="javascript:;" @click="changeTag(item,i)" :class="{active:activeIndex===i}" v-for="(item,i) in commentInfo.tags" :key="i">{{item.title}}({{item.tagCount}})</a>
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a :class="{active:reqParams.sortField===null}" @click="changeSort(null)" href="javascript:;" >默认</a>
      <a :class="{active:reqParams.sortField==='praiseCount'}" @click="changeSort('praiseCount')" href="javascript:;">最新</a>
      <a :class="{active:reqParams.sortField==='createTime'}" @click="changeSort('createTime')" href="javascript:;">最热</a>
    </div>
        <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="(item,i) in commentList" :key="i">
        <div class="user">
          <img v-lazy="item.member.avatar" alt="">
          <span>{{item.member.nickname}}</span>
        </div>
        <div class="body">
          <div class="score">
            <i class="iconfont icon-wjx01" v-for="item1 in item.score" :key="item1"></i>
            <i class="iconfont icon-wjx02" v-for="item1 in 5-item.score" :key="item1"></i>
            <span class="attr">{{item.orderInfo.specs.map(item=>`${item.name}:${item.nameValue}`).join(' ')}}</span>
          </div>
          <div class="text">{{item.content}}</div>
          <GoodsCommentImage v-if="item.pictures.length" :pictures='item.pictures'></GoodsCommentImage>
          <div class="time">
            <span>{{item.createTime}}</span>
            <span class="zan"><i class="iconfont icon-dianzan"></i>{{item.praiseCount}}</span>
          </div>
        </div>
      </div>
    </div>
    <XtxPagination @current-change='currentChangeFn' :total='total' :currentPage='reqParams.page' :pageSize='reqParams.pageSize'></XtxPagination>
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import { inject, ref, reactive, watch } from 'vue'
import GoodsCommentImage from './goods-comment-image.vue'
import XtxPagination from '@/components/library/xtx-pagination.vue'

export default {
  components: { GoodsCommentImage, XtxPagination },
  name: 'GoodsComment',
  setup () {
    const goods = inject('goods')
    const commentInfo = ref({})
    findGoodsCommentInfo(goods.value.id).then(({ result }) => {
      console.log(result)
      // 数据的处理，加上全部跟有图
      result.tags = [
        { title: '全部评价', tagCount: result.evaluateCount },
        { title: '有图', tagCount: result.hasPictureCount },
        ...result.tags
      ]
      commentInfo.value = result
      // goods 是响应式对象，修改的话可以更新到父组件组件上
      goods.value.commentCount = result.evaluateCount
    })
    const activeIndex = ref(0)
    const commentList = ref([])

    // 筛选条件准备
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null
    })
    // 点击排序
    const changeSort = (val) => {
      reqParams.sortField = val
      reqParams.page = 1
    }
    // 点击大家都说
    const changeTag = (theItem, i) => {
      activeIndex.value = i
      if (theItem.title === '全部评价') {
        reqParams.hasPicture = false
        reqParams.tag = null
      } else if (theItem.title === '有图') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = false
        reqParams.tag = theItem.title
      }
      reqParams.page = 1
    }
    const total = ref(0)
    // 监听对象发生变化
    watch(reqParams, () => {
      findGoodsCommentList(goods.value.id, reqParams).then(({ result }) => {
        console.log(result)
        total.value = result.counts
        commentList.value = result.items
      })
    }, { immediate: true })

    // 页码发生改变
    const currentChangeFn = (val) => {
      console.log(val)
      reqParams.page = val
    }
    return { goods, commentInfo, activeIndex, commentList, changeSort, reqParams, changeTag, currentChangeFn, total }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
    .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
}
</style>
